global.arr_to_tree = function(data, pid) {
  let result = []; 
  let temp;
  var length = data.length;
  for (var i = 0; i < length; i++) {
    if (data[i].pid === pid) {
      result.push(data[i]);
      temp = arr_to_tree(data, data[i].id);
      if (temp.length > 0) {
        data[i].children = temp;
        data[i].chnum = data[i].children.length;
      }
    }
  }
  return result;
};

/**
 * ip转数字
 * @param ip
 * @returns {number}
 * @private
 */
/* global _ip2int(ip) */
global._ip2int = function(ip) {
  var num = 0;
  ip = ip.split('.');
  num = Number(ip[0]) * 256 * 256 * 256 + Number(ip[1]) * 256 * 256 + Number(ip[2]) * 256 + Number(ip[3]);
  num = num >>> 0;
  return num;
};

/**
 * 密码加密
 * @param password 加密的密码
 * @param md5encoded true-密码不加密，默认加密
 * @returns {*}
 */
/* global encryptPassword */
global.encryptPassword = function(password, md5encoded) {
  md5encoded = md5encoded || false;
  password = md5encoded ? password : think.md5(password);
  return think.md5(think.md5('www.shiyuru.com') + password + think.md5('shiyu'));
};

/**
 * in_array
 * @param stringToSearch
 * @param arrayToSearch
 * @returns {boolean}
 */
/* global in_array */
global.in_array = function(stringToSearch, arrayToSearch) {
  for (let s = 0; s < arrayToSearch.length; s++) {
    const thisEntry = arrayToSearch[s].toString();
    if (thisEntry == stringToSearch) {
      return true;
    }
  }
  return false;
};
/**
 * 时间戳格式化 dateformat()
 * @param extra 'Y-m-d H:i:s'
 * @param date  时间戳
 * @return  '2015-12-17 15:39:44'
 */
/* global dateformat */
global.dateformat = function(extra, date) {
  const D = new Date(date);
  const time = {
    'Y': D.getFullYear(),
    'm': D.getMonth() + 1,
    'd': D.getDate(),
    'H': D.getHours(),
    'i': D.getMinutes(),
    's': D.getSeconds()
  };
  const key = extra.split(/\W/);
  let _date;
  for (const k of key) {
    time[k] = time[k] < 10 ? '0' + time[k] : time[k];
    _date = extra.replace(k, time[k]);
    extra = _date;
  }
  return _date;
};

/**
 * 
 * @param {* array } list 数据列表 
 * @param {* string } id id key 
 * @param {* string } pid pid key 
 * @param {* string } path 
 * @param {* string} ppath 
 */
global.arr2table = function(list, id='id', pid='pid', path='path', ppath='', f) {
  let tree;
  if (!f) {
    tree = arr_to_tree(list, 0);
  } else {
    tree = list;
  }
  let data = [];
  tree.forEach((item, index) => {
    item.path = `${ppath}-${item.id}`;
    item.children = item.children || [];
    item.sql = str_repeat("&nbsp;&nbsp;&nbsp;├&nbsp;&nbsp;", substr_count(ppath, '-'));
    let sub = [].concat(item.children);
    item.children = null;
    data.push(item)
    if (sub.length > 0) {
      data.push.apply(data, arr2table(sub, id, pid, path, item.path, true))
    }
  })
  return data;
}
global.arr2tree = function(list, id='id', pid='pid', son='sub') {
  let [tree, map] = [[], []];
  for(var i = 0; i < list.length; i++) {
    map[list[i][id]] = list;
  }
  for(var i = 0; i < list.length; i++) {
    let item = list[i];
    if (item[pid] && map[item[pid]]) {
      map[item[pid]][son] = map[item[pid]][son] || [];
      map[item[pid]][son].push(map[item[id]]);
    } else {
      tree.push(map[item[id]]);
    }
  }
  map = null;
  return tree;
}
/**
 * 获取字符在当前字符串中出现的次数
 * @param {* string } str 需要检查的字符串 
 * @param {* string } spl 检查字符串 
 */
global.substr_count = function(str, spl) {
  return str.split(spl).length - 1;
}

global.str_repeat = function(str, num) {
  return new Array( num + 1 ).join( str ); 
}

// global.getFilePaths(path, data=[], ext='js') {
//   for (const dir of path) {
    
//   }
// }